package com.lanhao.aot.carrierintegration.gwe;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.hutool.core.util.XmlUtil;
import cn.hutool.http.HttpRequest;

public class GweRoyalMailUtil {
	private static final Logger LOG = LoggerFactory.getLogger(GweRoyalMailUtil.class);
	public static final String API_URL = "http://oms.gw-express.com.cn/default/svc/web-service";
	public static final String APP_TOKEN = "03ee6ef7ee6150bcabb51b55f7fa3e8f";
	public static final String APP_KEY = "03ee6ef7ee6150bcabb51b55f7fa3e8f97d034d059e596b8e6392d3dc379f55d";
	public static final String SERVICE = "createOrder";
	public static final String SERVICE_PRINT = "getLabelUrl";
	public static final String SHIP_METHOD = "TEST-PD-CODE";
	
	public static void main(String[] args) {
//		String referenceNo = "ANHETEST000002";
//		createOrder(referenceNo);
		printLabel("GWE240113GWE002710000116");
	}
	
	public static void createOrder(String referenceNo) {
		
		JSONObject paramObject = new JSONObject();
		paramObject.put("reference_no", referenceNo);
		paramObject.put("shipping_method", SHIP_METHOD);
		paramObject.put("country_code", "GB");
		paramObject.put("order_weight", 0.3);
		paramObject.put("order_pieces", 1);
		paramObject.put("reference_id", "461983");
		paramObject.put("shipment_id", "1235646");
		paramObject.put("POANumber", "64613135");
		
		JSONObject Consignee = new JSONObject();
		Consignee.put("consignee_street", "Unit 2265,Unit 6,Charlton Gate Business Park,Anchor and Hope Lane,");
		Consignee.put("consignee_street2", "");
		Consignee.put("consignee_name", "UK-RTN-AE");
		Consignee.put("consignee_telephone", "+447897159358");
		Consignee.put("consignee_postcode", "SE7 7RU");
		Consignee.put("consignee_province", "England");
		Consignee.put("consignee_city", "London");
		paramObject.put("Consignee", Consignee);
		
		JSONObject Shipper = new JSONObject();
		Shipper.put("shipper_countrycode", "GB");
		Shipper.put("shipper_province", "England");
		Shipper.put("shipper_city", "London");
		Shipper.put("shipper_street", "189 Northfields Avenue");
		Shipper.put("shipper_postcode", "W139QU");
		Shipper.put("shipper_name", "Arpana Sunuwar");
		Shipper.put("shipper_telephone", "07428405089");
		Shipper.put("shipper_mobile", "");
		paramObject.put("Shipper", Shipper);
		
		JSONArray ItemArr = new JSONArray();
		JSONObject Item = new JSONObject();
		Item.put("invoice_cnname", "安和退货物品");
		Item.put("invoice_enname", "AnHeGoods");
		Item.put("invoice_weight", "0.3");
		Item.put("invoice_quantity", "1");
		Item.put("invoice_unitcharge", 0.01);
		Item.put("hs_code", "3506100001");
		Item.put("invoice_brand", "物品");
		Item.put("sku", "227652501");
		Item.put("box_number", "U001");
		ItemArr.add(Item);
		
		paramObject.put("ItemArr", ItemArr);
		
//		JSONArray Volume = new JSONArray();
//		JSONObject v = new JSONObject();
//		v.put("length", "10");
//		v.put("width", "10");
//		v.put("height", "10");
//		v.put("weight", "0.3");
//		v.put("box_number", "U001");
//		v.put("child_number", "TEST1234U001");
//		Volume.add(v);
//		paramObject.put("Volume", Volume);
		
//		<?xml version="1.0" encoding="UTF-8"?>
//		<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.example.org/Ec/">
//			<SOAP-ENV:Body>
//				<ns1:callService>			
//					<paramsJson>{json}</paramsJson>
//					<appToken>appToken</appToken>
//					<appKey>appKey</appKey>
//					<service>{service}</service>
//				</ns1:callService>
//			</SOAP-ENV:Body>
//		</SOAP-ENV:Envelope>
		
		StringBuilder request = new StringBuilder();
		request.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		request.append("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://www.example.org/Ec/\">");
		request.append("<SOAP-ENV:Body>");
		request.append("<ns1:callService>");
		request.append("<paramsJson>" + paramObject.toJSONString() + "</paramsJson>");
		request.append("<appToken>" + APP_TOKEN + "</appToken>");
		request.append("<appKey>" + APP_KEY + "</appKey>");
		request.append("<service>" + SERVICE + "</service>");
		request.append("</ns1:callService>");
		request.append("</SOAP-ENV:Body>");
		request.append("</SOAP-ENV:Envelope>");
		
		LOG.info("请求Gwe皇家邮政url={}， request={}", API_URL, request.toString());
		
		String respBody = HttpRequest.post(API_URL).body(request.toString()).execute().body();
		
		LOG.info("获取Gwe皇家邮政返回信息respBody={}", respBody);
		
//		<?xml version="1.0" encoding="UTF-8"?>
//		<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.example.org/Ec/"><SOAP-ENV:Body><ns1:callServiceResponse><response>{"ask":"Failure","message":"服务异常:订单数据不合法002","reference_no":"ANHETEST000001","Error":{"errMessage":"英文品名不能为空, 申报单价不能为空, 收件人地址1不能为空","errCode":"50048"},"time_cost(s)":0.10839796066284}</response></ns1:callServiceResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
		
		Map<String, Object> xmlToMap = XmlUtil.xmlToMap(respBody);
		
		LOG.info("将XML返回结果转换为Map信息，xmlToMap={}", JSON.toJSONString(xmlToMap));
//		{"SOAP-ENV:Body":{"ns1:callServiceResponse":{"response":"{\"ask\":\"Success\",\"message\":\"Success\",\"reference_no\":\"ANHETEST000001\",\"shipping_method_no\":\"GWE231113GWE002710000189\",\"order_code\":\"GWE231113GWE002710000189\",\"track_status\":3,\"sender_info_status\":0,\"ODA\":null,\"agent_number\":\"\",\"system_number\":null,\"tracking_number_list\":{\"U001\":\"GWE231113GWE002710000189\"},\"time_cost(s)\":0.701171875}"}}}
		JSONObject xmlObject = JSON.parseObject(JSON.toJSONString(xmlToMap));
		
		JSONObject SOAPBody = xmlObject.getJSONObject("SOAP-ENV:Body");
		JSONObject callServiceResponse = SOAPBody.getJSONObject("ns1:callServiceResponse");
		String resp = callServiceResponse.getString("response");
		JSONObject respObject = JSON.parseObject(resp);
		String ask = respObject.getString("ask");
		if ("Success".equals(ask)) {
			String shippingMethodNo = respObject.getString("shipping_method_no");
			LOG.info("英国皇家邮政下单成功，单号shippingMethodNo={}", shippingMethodNo);
		} else {
			String message = respObject.getString("message");
			JSONObject errorObject = respObject.getJSONObject("Error");
			String errMessage = errorObject.getString("errMessage");
			String errCode = errorObject.getString("errCode");
			LOG.info("下单失败，错误信息message={}，错误码errCode={}，详细错误信息errMessage={}", message, errCode, errMessage);
			
		}
		
	}
	
	
	public static void printLabel(String referenceNo) {
		JSONObject paramObject = new JSONObject();
		paramObject.put("reference_no", referenceNo);
		paramObject.put("label_type", "3");
		
		StringBuilder request = new StringBuilder();
		request.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		request.append("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://www.example.org/Ec/\">");
		request.append("<SOAP-ENV:Body>");
		request.append("<ns1:callService>");
		request.append("<paramsJson>" + paramObject.toJSONString() + "</paramsJson>");
		request.append("<appToken>" + APP_TOKEN + "</appToken>");
		request.append("<appKey>" + APP_KEY + "</appKey>");
		request.append("<service>" + SERVICE_PRINT + "</service>");
		request.append("</ns1:callService>");
		request.append("</SOAP-ENV:Body>");
		request.append("</SOAP-ENV:Envelope>");
		
		String respBody = HttpRequest.post(API_URL).body(request.toString()).execute().body();
		
		LOG.info("获取标签信息resp={}", respBody);
		
//		获取标签信息resp=<?xml version="1.0" encoding="UTF-8"?>
//		<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.example.org/Ec/"><SOAP-ENV:Body><ns1:callServiceResponse><response>{"ask":"Success","message":"Success","Error":{"errCode":"","errMessage":""},"type":"pdf","url":"http:\/\/oms.gw-express.com.cn\/PDF\/7b7fdf91-82cd-462f-814e-1497361949a2.pdf","reference_no":"ANHETEST000001","time_cost(s)":1.8798830509186}</response></ns1:callServiceResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>

	}
}
